﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class DocumentoClienteAlmacenDetalleDatos

    Sub registrarDocumentoClienteAlmacenDetalle(dcad As DocumentoClienteAlmacenDetalle)
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "INSERT INTO Documento_Cliente_Almacen_Detalle(CIA_Cliente,SUC_Cliente, " &
                                  "TDOC_Cliente,DCI_Codigo,DCD_Codigo,CLI_Codigo,CIA_Almacen,SUC_Almacen,TDOC_Almacen, " &
                                  "DCA_Codigo,DAD_Codigo,ALM_Codigo,DCAD_Activo) " &
                                  "VALUES(@ciaC,@sucC,@tdocC,@dciCodigo,@dcdCodigo,@cliente,@ciaA,@sucA,@tdocA,@dcaCodigo, " &
                                  "@dadCodigo,@almCodigo,'1')"

            Dim comando As New SqlCommand
            With comando
                .Connection = coneccion
                .CommandText = query
                .Parameters.AddWithValue("@ciaC", dcad.CIA_Cliente)
                .Parameters.AddWithValue("@sucC", dcad.SUC_Cliente)
                .Parameters.AddWithValue("@tdocC", dcad.TDOC_Cliente)
                .Parameters.AddWithValue("@dciCodigo", dcad.DCI_Codigo)
                .Parameters.AddWithValue("@dcdCodigo", dcad.DCD_Codigo)
                .Parameters.AddWithValue("@cliente", dcad.CLI_Codigo)
                .Parameters.AddWithValue("@ciaA", dcad.CIA_Almacen)
                .Parameters.AddWithValue("@sucA", dcad.SUC_Almacen)
                .Parameters.AddWithValue("@tdocA", dcad.TDOC_Almacen)
                .Parameters.AddWithValue("@dcaCodigo", dcad.DCA_Codigo)
                .Parameters.AddWithValue("@dadCodigo", dcad.DAD_Codigo)
                .Parameters.AddWithValue("@almCodigo", dcad.ALM_Codigo)
                .ExecuteNonQuery()
            End With

        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

    Sub desactivarRelacionDocumentoClienteAlmacenDetalle(cia As String, suc As String, alm As String, tdoc As String, dcaCodigo As String, dadCodigo As String)
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "UPDATE Documento_Cliente_Almacen_Detalle " &
                                  "SET DCAD_Activo = '0' " &
                                  "WHERE CIA_Almacen = @cia AND SUC_Almacen = @suc " &
                                  "AND ALM_Codigo = @alm AND DCA_Codigo = @dcaCodigo " &
                                  "AND DAD_Codigo = @dadCodigo AND DCAD_Activo = '1'"

            Dim comando As New SqlCommand
            With comando
                .Connection = coneccion
                .CommandText = query
                .Parameters.AddWithValue("@cia", cia)
                .Parameters.AddWithValue("@suc", suc)
                .Parameters.AddWithValue("@alm", alm)
                .Parameters.AddWithValue("@tipo", tdoc)
                .Parameters.AddWithValue("@dcaCodigo", dcaCodigo)
                .Parameters.AddWithValue("@dadCodigo", dadCodigo)
                .ExecuteNonQuery()
            End With

        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

End Class
